package com.zdb.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static com.zdb.easyexcel.Vo.getVoList;

public class Test {
    public static void main(String[] args) {
        readByByte();
    }
    void  test01(){
        String filename="D:/test.xlsx";
        List<Vo> voList = getVoList();
        EasyExcel.write(filename,Vo.class)
                .sheet("用户信息")
                .doWrite(voList);
    }
     public  static void  test02(){
        String filename="D:/test.xlsx";
        List<Vo> voList = getVoList();
        HashSet<String> strings = new HashSet<>();
        strings.add("age");
        EasyExcel.write(filename,Vo.class)
                .excludeColumnFieldNames(strings)
                .sheet("用户信息")
                .doWrite(voList);
    }
    public  static void test03(){
        String filename="D:/test.xlsx";
        ExcelWriter build = EasyExcel.write(filename, Vo.class).build();
        WriteSheet build1 = EasyExcel.writerSheet("用户信息").build();
        build.write(getVoList(), build1);
        build.finish();
    }
    public static  void testRead(){
        String filename="D:/test.xlsx";
        ExcelReaderBuilder read = EasyExcel.read(filename);
        read.sheet(0);
        read.autoCloseStream(true);
        read.excelType(ExcelTypeEnum.XLSX);
        read.registerReadListener(new AnalysisEventListener<Object>() {
            @Override
            public void invoke(Object o, AnalysisContext analysisContext) {

            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        });
        read.doReadAll();
    }
    public static void readByByte(){
        String filename="D:/test.xlsx";
        ArrayList<Vo> vos = new ArrayList<>();
        Class<Vo> voClass = Vo.class;
        ExcelReader reader = EasyExcel.read(filename, voClass, new AnalysisEventListener() {
            @Override
            public void invoke(Object o, AnalysisContext analysisContext) {
                Vo t = (Vo) o;
                vos.add(t);
                System.out.println("当前解析数据是" + t.toString());
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("解析完成");
            }
        }).build();
        ReadSheet sheet = EasyExcel.readSheet(0).build();
        reader.read(sheet);
        reader.finish();
        vos.forEach(System.out::println);
    }

}
